package com.wuliner.unlock_new

import android.content.Context
import android.media.metrics.Event
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.wuliner.unlock_new.databinding.ActivityMainBinding

class DotsView(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {
    init {
        LayoutInflater.from(context).inflate(R.layout.dots_view, this)
    }

    private val selectedViewList = arrayListOf<ImageView>()
    private val dotArray = listOf<ImageView>(
        findViewById(R.id.dot1),
        findViewById(R.id.dot2),
        findViewById(R.id.dot3),
        findViewById(R.id.dot4),
        findViewById(R.id.dot5),
        findViewById(R.id.dot6),
        findViewById(R.id.dot7),
        findViewById(R.id.dot8),
        findViewById(R.id.dot9)
    )
    private var selectedDotList = arrayListOf<ImageView>()
    private val pwdBuilder = StringBuilder()

    //添加已选视图
    fun addSelectedView(imageView: ImageView) {
        selectedViewList.add(imageView)
    }

    //改变图片状态
    fun changeState(state: Boolean) {
        if (state) {
            selectedViewList.forEach {
                val viewTag = (it.tag as String).toInt()
                if (viewTag / 10 == 0) {
                    it.setImageResource(R.drawable.dot_selected)
                } else {
                    when (viewTag % 10 - viewTag / 10) {
                        1 -> {
                            it.setImageResource(R.drawable.line_1_normal)
                        }
                        2 -> {
                            it.setImageResource(R.drawable.line_3_normal)
                        }
                        3 -> {
                            it.setImageResource(R.drawable.line_2_normal)
                        }
                        4 -> {
                            it.setImageResource(R.drawable.line_4_normal)
                        }
                    }
                }
            }
        } else {
            selectedViewList.forEach {
                val viewTag = (it.tag as String).toInt()
                if (viewTag / 10 == 0) {
                    it.setImageResource(R.drawable.dot_error)
                } else {
                    when (viewTag % 10 - viewTag / 10) {
                        1 -> {
                            it.setImageResource(R.drawable.line_1_error)
                        }
                        2 -> {
                            it.setImageResource(R.drawable.line_3_error)
                        }
                        3 -> {
                            it.setImageResource(R.drawable.line_2_error)
                        }
                        4 -> {
                            it.setImageResource(R.drawable.line_4_error)
                        }
                    }
                }
            }
        }

    }
    fun changeState(list: List<ImageView>, state: Boolean) {
        if (state) {
            list.forEach {
                val viewTag = (it.tag as String).toInt()
                if (viewTag / 10 == 0) {
                    it.setImageResource(R.drawable.dot_selected)
                } else {
                    when (viewTag % 10 - viewTag / 10) {
                        1 -> {
                            it.setImageResource(R.drawable.line_1_normal)
                        }
                        2 -> {
                            it.setImageResource(R.drawable.line_3_normal)
                        }
                        3 -> {
                            it.setImageResource(R.drawable.line_2_normal)
                        }
                        4 -> {
                            it.setImageResource(R.drawable.line_4_normal)
                        }
                    }
                }
            }
        } else {
            list.forEach {
                val viewTag = (it.tag as String).toInt()
                if (viewTag / 10 == 0) {
                    it.setImageResource(R.drawable.dot_error)
                } else {
                    when (viewTag % 10 - viewTag / 10) {
                        1 -> {
                            it.setImageResource(R.drawable.line_1_error)
                        }
                        2 -> {
                            it.setImageResource(R.drawable.line_3_error)
                        }
                        3 -> {
                            it.setImageResource(R.drawable.line_2_error)
                        }
                        4 -> {
                            it.setImageResource(R.drawable.line_4_error)
                        }
                    }
                }
            }
        }
    }

    //改变图片可见
    fun changeVisibility(state: Boolean) {
        if (state) {
            selectedViewList.forEach {
                it.visibility = View.VISIBLE
            }
        } else {
            selectedViewList.forEach {
                it.visibility = View.INVISIBLE
            }
        }
    }
    fun changeVisibility(list: List<ImageView>, state: Boolean){
        if (state) {
            list.forEach {
                it.visibility = View.VISIBLE
            }
        } else {
            list.forEach {
                it.visibility = View.INVISIBLE
            }
        }
    }

    //清除已选中点
    fun clearDotList(){
        selectedDotList.clear()
    }

    //清除已选视图
    fun clearViewList() {
        selectedViewList.clear()
    }

    //event是否在点内
    fun isInDot(event: MotionEvent):ImageView?{
        dotArray.forEach{
            if((event.x>it.left&&event.y<it.right)&&(event.y>it.top&&event.y<it.bottom)){
                return it
            }
        }
        return null
    }

    //添加已选中点
    fun addSelectedDot(dot: ImageView){
        selectedDotList.add(dot)
    }
    fun addSelectedDot(dotList:List<ImageView>){
        selectedDotList = dotList.toList() as ArrayList<ImageView>
    }

    //密码生成器
    fun passwordBuilder():String{
        selectedDotList.forEach {
            pwdBuilder.append(it.tag as String)
        }
        return pwdBuilder.toString()
    }
}

